<template>
  <a-input-search
    v-model:value="searchText"
    placeholder="请输入关键词搜索"
    enter-button
    @search="handleSearch"
    @input="handleInput"
  />
</template>

<script setup>
import { ref, watch } from 'vue'
import { debounce } from 'lodash'
import { useSearchStore } from '@/stores/search/search'
import { useRouter } from 'vue-router'

const router = useRouter()
const searchText = ref('')
const loading = ref(false)

const searchResults = ref([])
const searchStore = useSearchStore()

// 防抖处理（300ms）
const debouncedSearch = debounce(() => {
  handleSearch()
}, 300)

// 输入处理
const handleInput = () => {
  if (searchText.value.trim()) {
    debouncedSearch()
  }
}

// 执行搜索
const handleSearch = async () => {
  try {
    loading.value = true
    const keyword = searchText.value.trim()

    router.push({
      path: '/searchlist',
      query: {
        keyword,
      },
    })
  } catch (error) {
    console.error('搜索失败:', error)
    message.error('搜索失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>
<style lang="less" scoped></style>
